package com.sub.project.mall.service.impl;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.UpdateChainWrapper;
import com.sub.common.utils.ServletUtils;
import com.sub.common.utils.StringUtils;
import com.sub.framework.security.LoginUser;
import com.sub.framework.security.service.TokenService;
import com.sub.framework.web.domain.TreeSelect;
import com.sub.project.mall.domain.PmsProductCategory;
import com.sub.project.mall.mapper.PmsProductCategoryMapper;
import com.sub.project.mall.service.IPmsProductCategoryService;
import com.sub.project.system.domain.SysDept;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-08-03
 */
@Service
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory> implements IPmsProductCategoryService
{
    @Autowired
    private PmsProductCategoryMapper pmsProductCategoryMapper;
    @Autowired
    private TokenService tokenService;
    @Override
    public List<PmsProductCategory> list(PmsProductCategory pmsProductCategory) {
        /** 查询*/
        QueryWrapper<PmsProductCategory> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pmsProductCategory.getName())){
            queryWrapper.eq("name",pmsProductCategory.getName());
        }
        if(StringUtils.isNotEmpty(pmsProductCategory.getKeywords())){
            queryWrapper.eq("keywords",pmsProductCategory.getKeywords());
        }
        if(!StringUtils.isNull(pmsProductCategory.getParentId())){
            queryWrapper.eq("parent_id",pmsProductCategory.getParentId());
        }

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        queryWrapper.select("id","parent_id","name","create_name","level");
        queryWrapper.eq("create_by_id",loginUser.getUser().getUserId());
        return  pmsProductCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<PmsProductCategory> depts) {
        List<PmsProductCategory> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<PmsProductCategory> buildDeptTree(List<PmsProductCategory> categories) {
        List<PmsProductCategory> returnList = new ArrayList<PmsProductCategory>();
        if (StringUtils.isNotEmpty(categories) && StringUtils.isNotNull(categories.stream().findFirst()))
        {
            categories.stream().findFirst().get().setParentId(null);
        }
        for (Iterator<PmsProductCategory> iterator = categories.iterator(); iterator.hasNext();)
        {
            PmsProductCategory t = (PmsProductCategory) iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (StringUtils.isNull(t.getParentId()) || t.getParentId() == 0)
            {
                recursionFn(categories, t);
                returnList.add(t);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = categories;
        }
        return returnList;
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<PmsProductCategory> list, PmsProductCategory t)
    {
        // 得到子节点列表
        List<PmsProductCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (PmsProductCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                // 判断是否有子节点
                Iterator<PmsProductCategory> it = childList.iterator();
                while (it.hasNext())
                {
                    PmsProductCategory n = (PmsProductCategory) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }


    /**
     * 得到子节点列表
     */
    private List<PmsProductCategory> getChildList(List<PmsProductCategory> list, PmsProductCategory t)
    {
        List<PmsProductCategory> tlist = new ArrayList<PmsProductCategory>();
        Iterator<PmsProductCategory> it = list.iterator();
        while (it.hasNext())
        {
            PmsProductCategory n = (PmsProductCategory) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     *  判断是否存在字节点
     * @param list
     * @param t
     * @return
     */
    private boolean hasChild(List<PmsProductCategory> list, PmsProductCategory t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
